﻿using System;
using System.Collections.Generic;
using Microsoft.SharePoint;
using Sapphire.Linq.Framework;

namespace Sapphire.Linq.Tracker
{
    public class ItemsTracker : IItemsTracker
    {
        private readonly LinqQueryProvider _queryProvider;

        private readonly Dictionary<Guid, object> _items = new Dictionary<Guid, object>();

        private T Register<T>(SPListItem spListItem, T entity)
        {
            if (_items.ContainsKey(spListItem.UniqueId))
                _items[spListItem.UniqueId] = entity;
            else
                _items.Add(spListItem.UniqueId, entity);

            return entity;
        }

        public ItemsTracker(LinqQueryProvider queryProvider)
        {
            _queryProvider = queryProvider;
        }

        public SPListItem GetItem(Guid uniqueId)
        {
            return _queryProvider.Web.GetFile(uniqueId).Item;
        }
    }
}